package edu.iscas.appcheckclient.service.download;

import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.util.Log;

import java.io.File;

import edu.iscas.appcheckclient.R;

/**
 * Created by tangbo on 16/9/5.
 */
public class DownLoadService extends Service {
    private static final String TAG = DownLoadService.class.getSimpleName();
    String download_url;
    String savePath= "";
    String apkName="";
    private int requestCode = (int) SystemClock.uptimeMillis();
    private NotifyUtil currentNotify;
    File mFile;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();


    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mFile=new File(savePath);
        download_url=intent.getStringExtra("download_url");
        apkName = intent.getStringExtra("fileName");
        Log.e(TAG,"onStartCommand");
        Log.e(TAG, "url:"+download_url);
        savePath = Environment.getExternalStorageDirectory()+"/AppCheckClient";
        File file = new File(savePath);
        if (!file.exists()) {
            Log.e(TAG, "create file:"+savePath);
            file.mkdirs();
        }
        savePath = savePath+"/" + intent.getStringExtra("fileName");
        Log.e(TAG,"savePath:"+savePath);
        //设置想要展示的数据内容
        Intent intent_noti = new Intent();
        intent_noti.setAction(Intent.ACTION_VIEW);
        //文件的类型，从tomcat里面找
        intent_noti.setDataAndType(Uri.fromFile(mFile), "application/vnd.android.package-archive");
        PendingIntent rightPendIntent = PendingIntent.getActivity(this, requestCode, intent_noti, PendingIntent.FLAG_UPDATE_CURRENT);
        int smallIcon = R.drawable.ic_launcher;
        String ticker = apkName;
        //实例化工具类，并且调用接口
        NotifyUtil notify7 = new NotifyUtil(this, 7);
        notify7.notify_progress(rightPendIntent, smallIcon, ticker, "Download "+apkName, "Downloading",
                false, false, false, download_url, savePath, new NotifyUtil.DownLoadListener() {
                    @Override
                    public void OnSuccess(File file) {
                        mFile=file;
                        DownLoadService.this.stopSelf();
                    }

                    @Override
                    public void onFailure(Throwable t, int errorNo, String strMsg) {

                    }
                });
        currentNotify = notify7;
        return super.onStartCommand(intent, flags, startId);
    }
}
